from rest_framework import serializers
from rest_framework.serializers import Serializer, ModelSerializer

from booktest.models import BookInfo


class BookInfoSerializer(Serializer):
    '''
    书本序列化器
    '''
    id = serializers.IntegerField(label='ID', read_only=True)
    btitle = serializers.CharField(label='书本名称', max_length=20)
    bpub_date = serializers.DateTimeField(label='发布日期')
    bread = serializers.IntegerField(label='阅读量', required=False)
    bcomment = serializers.IntegerField(label='评论量', required=False)
    # 一对多关联序列化字段
    heroinfo_set = serializers.StringRelatedField(label='关联的英雄', many=True, required=False)

    def create(self, validated_data):
        '''
        新建
        :param validated_data:
        :return:
        '''

        return BookInfo(**validated_data)
        pass

    def update(self, instance, validated_data):
        '''
        更新
        :param instance: 要更新的对象
        :param validated_data: 数据
        :return:
        '''

        instance.btitle = validated_data.get('btitle', instance.btitle)
        instance.bpub_date = validated_data.get('bpub_date', instance.bpub_date)
        instance.bread = validated_data.get('bread', instance.bread)
        instance.bcomment = validated_data.get('bcomment', instance.bcomment)

        instance.save()
        return instance
        pass


class BookInfo01Serializer(ModelSerializer):
    '''
    指定所有字段的序列化器
    '''
    class Meta:
        model = BookInfo
        fields = '__all__'

class BookInfo02Serializer(ModelSerializer):
    '''
    指定字段
    '''

class HeroInfoSerializer(Serializer):
    '''
    英雄序列化器
    '''
    GENDER_CHOICES = (
        (0, 'female'),
        (1, 'male'),
    )

    id = serializers.IntegerField(label='ID', read_only=True)
    hname = serializers.CharField(max_length=20, label='名称')
    hgender = serializers.ChoiceField(choices=GENDER_CHOICES, label='性别', required=False)
    hcomment = serializers.CharField(max_length=200, label='描述信息', required=False)
    # hbook = serializers.PrimaryKeyRelatedField(label='所在图书', read_only=True)
    # hbook = serializers.PrimaryKeyRelatedField(label='图书', queryset=BookInfo.objects.all())
    hbook = serializers.StringRelatedField(label='图书')

    pass



class Usage():
    '''
    序列化器使用的代码
    '''

    def aaa(self):

        from booktest.models import BookInfo
        from booktest.serializers import BookInfoSerializer

        # 单个序列化
        book = BookInfo.objects.get(id=2)
        serializer = BookInfoSerializer(instance=book)
        print(serializer.data)

        # 获取查询集：
        books = BookInfo.objects.all()
        serializer = BookInfoSerializer(instance=books, many=True)
        print(serializer.data)

        pass

    def bbb(self):
        '''
        关联序列化：关联一的一方
        :return:
        '''

        from booktest.models import HeroInfo
        from booktest.serializers import HeroInfoSerializer

        hero = HeroInfo.objects.get(id=6)
        serializer = HeroInfoSerializer(instance=hero)
        print(serializer.data)
        pass

    def ccc(self):
        '''
        一对多关联序列化
        :return:
        '''

        from booktest.models import BookInfo
        from booktest.serializers import BookInfoSerializer

        book = BookInfo.objects.get(id=1)
        serializer = BookInfoSerializer(instance=book)
        print(serializer.data)
        pass

    def ddd(self):
        '''
        反序列化器验证
        :return:
        '''

        from booktest.serializers import BookInfoSerializer

        book_dict = {'btitle' : 'hahah'}
        serializer = BookInfoSerializer(data=book_dict)

        # 验证序列化器字段
        print(serializer.is_valid())
        # 输出序列化器错误信息
        print(serializer.errors)
        pass

    def eee(self):
        '''
        反序列化器验证
        :return:
        '''
        from booktest.serializers import BookInfoSerializer

        book_dict = {
            'btitle' : 'hahah',
            'bpub_date' : '1999-12-01',
        }
        serializer = BookInfoSerializer(data=book_dict)

        # 验证序列化器字段
        print(serializer.is_valid())

        print(serializer.errors)

        # 输出反序列化的内容
        print(serializer.validated_data)

        pass

    def fff(self):
        '''
        演示 ModelSerializer 的使用
        :return:
        '''

        from booktest.serializers import BookInfo01Serializer

        book_dict = {
            'btitle' : 'hahah',
            'bpub_date' : '1999-12-01T10:10:10',
        }
        serializer = BookInfo01Serializer(instance=book_dict)

        print(serializer.data)
        pass



# ===============================================================================







